﻿using MS.InformationManagement.Presenter.Support;
using MS.InformationManagement.BLL.Correspondence;
using MS.InformationManagement.Support;
using Microsoft.SharePoint;
using System.Linq;
using MS.InformationManagement.Support.Constants;
using MS.InformationManagement.Core.Support;
using MS.InformationManagement.BLL.DocumentPrinterCommands;
using MS.InformationManagement.Core.TransactionsResponse;
using System.Collections.Generic;
using System.Linq.Expressions;
using System;


namespace MS.InformationManagement.Presenter.DocumentPrinter
{
    public class PrintableDocumentListPresenter:BasePresenter<IPrintableDocumentListView>
    {

        #region fields

        Dictionary<string, string> _queryCommands;
        Dictionary<string, ICommandWithResponse> _queryCommandMethods;

        #endregion

        #region Constructors

        public PrintableDocumentListPresenter(IPrintableDocumentListView view) : base(view) 
        {
            _queryCommands = new Dictionary<string, string>();
            _queryCommandMethods = new Dictionary<string, ICommandWithResponse>();


            //Configuring libraries
            _queryCommands.Add("NonFilled", SPContext.Current.Web.CurrentUser.LoginName.GetUserWithOutDomain());
            _queryCommands.Add("Filled", PeopleHelper.GetPropertyUserProfile(GlobalConstants.UP_DEPARTMENT, SPContext.Current.Web.CurrentUser.LoginName));

            //Configuring query methods
            if (!_queryCommands.ContainsKey(view.Operation)) return;
            _queryCommandMethods.Add("NonFilled", new GetNonPrintedDocumentsLibraryCommand(_queryCommands[view.Operation], view.PageIndex, view.PageSize));
            _queryCommandMethods.Add("Filled", new GetNonPrintedDocumentsLibraryCommand(_queryCommands[view.Operation], view.PageIndex, view.PageSize));
        }

        #endregion

        #region Public Methods

        public void HandleLoad()
        {
            int totalRecords = 0;
            View.PageIndex = 0;
            View.TotalRecords = totalRecords;

            if (!_queryCommandMethods.ContainsKey(View.Operation)) return;

            ICommandWithResponse command = _queryCommandMethods[View.Operation];
            ITransactionResponse response = command.Execute();
            View.TotalRecords = ((PrinterDocumentsResponse)response).TotalRecords;
            View.Documents = ((PrinterDocumentsResponse)response).DocumentsToPrint;


            //else if(View.Operation == "Filled")
            //    View.Documents = DocumentPrinterBLL.GetRecordDocuments(currentLoginName, View.PageIndex, View.PageSize, out nonfiledTotalRecords).ToList();

            //if(View.Operation == "NonFilled")
            //    View.Documents = DocumentPrinterBLL.GetLibraryDocuments(currentLoginName, View.PageIndex, View.PageSize, out nonfiledTotalRecords).ToList();
            //else if(View.Operation == "Filled")
            //    View.Documents = DocumentPrinterBLL.GetRecordDocuments(currentLoginName, View.PageIndex, View.PageSize, out nonfiledTotalRecords).ToList();
            //else if(View.Operation == "Printed")
            //    View.Documents = DocumentPrinterBLL.GetPrintedDocuments(currentLoginName, View.PageIndex, View.PageSize).ToList();
            
            View.Show();
        }

        public void MarkUserDocumentAsPrinted(string userName)
        {
            string libraryName = View.Operation == "NonFilled" ? userName.GetUserWithOutDomain() : PeopleHelper.GetPropertyUserProfile(GlobalConstants.UP_DEPARTMENT, userName.GetUserWithOutDomain());
            View.SelectedDocumentPath = DocumentPrinterBLL.SetDocumentPrintDate(libraryName, View.SelectedItemId, View.SelectedContentType);
            View.ShowDocument();
        }

        public void MarkRecordDocumentAsPrinted(string userName)
        {
            View.Show();
        }


        #endregion


    }
}
